package ntu.p2p.dht.messaging;

import java.io.IOException;

import ntu.p2p.dht.DHTContent;
import ntu.p2p.dht.DHTContentDeserializer;
import ntu.p2p.past.messaging.ObjectDifference;
import ntu.p2p.past.test.Diff;
import rice.p2p.commonapi.Endpoint;
import rice.p2p.commonapi.Message;
import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.OutputBuffer;
import rice.p2p.commonapi.rawserialization.RawMessage;

public class InsertObjectMessage implements ObjectDifference {

	public static final short TYPE = 1;
	private static final long serialVersionUID = 7192145649923842058L;
	
	private DHTContent content;
	
	public InsertObjectMessage(DHTContent content) {
		this.content = content;
	}
	
	protected InsertObjectMessage(final InputBuffer buf, DHTContentDeserializer deserializer, Endpoint endpoint) 
			throws IOException {
		this.content = deserializer.deserialize(buf, endpoint);
	}
	
	@Override
	public void serialize(OutputBuffer buf) throws IOException {
		buf.writeShort(TYPE);
		// buf.writeShort(this.content.getKey().getId().getType());
		// this.content.getKey().getId().serialize(buf);
		this.content.serialize(buf);
	}
	
	public static ObjectDifference build(final InputBuffer buf, 
			DHTContentDeserializer deserializer, Endpoint endpoint) {
		try {
			return new InsertObjectMessage(buf, deserializer, endpoint);
		} catch (IOException e) {
			return null;
		}
	}

	public DHTContent getContent() {
		return this.content;
	}
}
